home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / p4 / p4-1_2a.lha / p4-1.2a / messages / sr1_test.c < prev    next >
C/C++ Source or Header  |  1992-12-15  |  2KB  |  119 lines

  1. #include "p4.h"
  2. #include "sr_user.h"
  3.  
  4. #define MAX_MESSAGE_SIZE 1500000
  5. char msg[MAX_MESSAGE_SIZE];
  6.  
  7. int main(argc,argv)
  8. int argc;
  9. char **argv;
  10. {
  11.  
  12.     p4_initenv(&argc,argv);
  13.     if (p4_get_my_id() == 0)
  14.     {
  15.         p4_create_procgroup();
  16.     master();
  17.     }
  18.     else
  19.     {
  20.         slave();
  21.     }
  22.   
  23.     p4_wait_for_end();
  24. }
  25.  
  26.     
  27. master()
  28. {
  29.     int nslaves;
  30.     int type, size, id, from;
  31.     int my_id;
  32.     char *incoming;
  33.     int done;
  34.     int msgsize, count;
  35.     int starttime, endtime;
  36.     p4_usc_time_t start_ustime, end_ustime;
  37.  
  38.     nslaves = p4_num_total_slaves();
  39.     printf("number of slaves = %d\n",nslaves);
  40.     my_id = p4_get_my_id();
  41.     
  42.     done = FALSE;
  43.     while (!done)
  44.     {
  45.     printf("message size: ");
  46.     scanf("%d",&msgsize);
  47.     if (msgsize > MAX_MESSAGE_SIZE)
  48.     {
  49.         printf("too big;  using %d\n",MAX_MESSAGE_SIZE);
  50.         msgsize = MAX_MESSAGE_SIZE;
  51.     }
  52.     printf("times around loop (or 0 for end): ");
  53.     scanf("%d",&count);
  54.     
  55.     if (count == 0)
  56.         done = TRUE;
  57.     else
  58.     {
  59.         starttime = p4_clock();
  60.         start_ustime = p4_ustimer();
  61.         while (count > 0)
  62.         {
  63.         p4_sendr(DATA, 1, msg, msgsize);
  64.         type = -1;
  65.         from = -1;
  66.         incoming = NULL;
  67.         p4_recv(&type, &from, &incoming, &size);
  68.         p4_msg_free(incoming);
  69.         count--;
  70.         }
  71.         end_ustime = p4_ustimer();
  72.         endtime = p4_clock();
  73.         printf("time %d milliseconds\n",endtime-starttime);
  74.         printf("time %d microseconds\n",end_ustime-start_ustime);
  75.     }
  76.     }
  77.  
  78.     p4_sendr(END, 1, msg, 0);
  79.     type = -1;
  80.     from = -1;
  81.     incoming = NULL;
  82.     p4_recv(&type, &from, &incoming, &size);
  83.     p4_msg_free(incoming);
  84.     p4_wait_for_end();
  85.     printf("master exiting normally\n");
  86. }
  87.  
  88. slave()    
  89. {
  90.     int nslaves;
  91.     int done;
  92.     int type, from, size;
  93.     int next;
  94.     int my_id;
  95.     char *incoming;
  96.     
  97.     my_id = p4_get_my_id();
  98.     nslaves = p4_num_total_slaves();
  99.     
  100.     if (my_id == nslaves)
  101.         next = 0;
  102.     else
  103.     next = my_id + 1;
  104.     
  105.     done = FALSE;
  106.     while (!done)
  107.     {
  108.     type = -1;
  109.     from = -1;
  110.     incoming = NULL;
  111.     p4_recv(&type,&from, &incoming, &size);
  112.     if (type == END)
  113.         done = TRUE;
  114.     p4_sendr(type, next, incoming, size);
  115.     p4_msg_free(incoming);
  116.     }
  117. }
  118.  
  119.